#include "stdafx.h"
#include "ServerCallbackGame.h"

ServerCallbackGame::ServerCallbackGame()
{
	RegisterHandler(0,boost::bind(&ServerCallbackGame::phPing_0, this,_1));//ping
}
/*ServerCallbackGame::ServerCallbackGame(ISocketServer *pSocketServer)
	:m_pSocketServer(pSocketServer)
{

}*/

ServerCallbackGame::~ServerCallbackGame()
{
	
}

void ServerCallbackGame::OnRun()
{
}

void ServerCallbackGame::OnRecvData(UInt32 nSocketId, const void* pData, size_t nDataLen)
{

	PacketDecoder decoder;
	if(!decoder.SetData(reinterpret_cast<const char*>(pData), nDataLen + 4))
	{
		LOGGER_ERROR("!decoder.SetData(reinterpret_cast<const char*>(pData), nDataLen + 4)");
		return;
	}

	if(decoder.GetItemCount() < 3)
	{
		LOGGER_ERROR("decoder.GetItemCount() < 3");
		return;
	}

	Int32 nSocketIdOnGate = 0;
	Int32 type = 0;
	Int32 id = 0;

	if(!decoder.GetItem(0, nSocketIdOnGate))
	{
		LOGGER_ERROR("!decoder.GetItem(0, nSocketId)");
		return;
	}

	if(!decoder.GetItem(1, type))
	{
		LOGGER_ERROR("!decoder.GetItem(1, type)");
		return;
	}

	if(!decoder.GetItem(2, id))
	{
		LOGGER_ERROR("!decoder.GetItem(1, id)");
		return;
	}

#ifdef _WIN32
	if(!(type == 0 && id == 0))
		printf("ServerCallbackGame OnRecvData nSocketIdOnGate:%d,type:%d,id:%d\n",nSocketIdOnGate,type,id);
#endif

	if(id < 0 || id >= 65536)
	{
		LOGGER_ERROR("id < 0 || id >= 65536",id);
		return;
	}

	PacketHandler method = GetHandler(id);
	if(method == NULL)
	{
		#ifdef _WIN32
		LOGGER_ERROR("error :method id:%d,is unimplement",id);
		#endif
	}
	else
	{
		static PacketHandlerParamGame param;
		param.stPlayerId.nSocketId = nSocketId;
		param.stPlayerId.nSocketIdOnGate = nSocketIdOnGate;
		param.pDecoder = &decoder;
		method((void*)&param);
	}
}
void ServerCallbackGame::phPing_0(void *pVoid)
{
	//LOGGER_INFO("ServerCallbackGame::phPing_0");
}

void ServerCallbackGame::OnNewConnection(UInt32 nSocketId,const std::string& strIp)
{
	singleton<GateManager>::instance().Add(nSocketId);
	LOGGER_INFO("OnNewConnection:%d,ip:%s",nSocketId,strIp.c_str());
}

void ServerCallbackGame::OnCloseConnection(UInt32 nSocketId,const std::string& strIp)
{
	singleton<GateManager>::instance().Remove(nSocketId);
	LOGGER_INFO("OnCloseConnection:%d,ip:%s",nSocketId,strIp.c_str());
}

void ServerCallbackGame::OnStop()
{
}

void ServerCallbackGame::OnUpdate()
{

}

void ServerCallbackGame::SendPacket(UInt32 nSocketId,PacketEncoder& encoder)
{
	static ISocketServer* pSocketServer = GetSocketServer();
	void   *p = const_cast<void*>(encoder.GetData());
	pSocketServer->SendData(nSocketId,(char*)p+sizeof(UInt32),encoder.GetDataSize()-sizeof(UInt32));
}

void ServerCallbackGame::SendPacket(UInt32 nSocketId,std::list<Int32>& listSocketId,PacketEncoder& encoder)
{
	PacketEncoder encoderSend;
	encoderSend.AddItem(Int32(listSocketId.size()));
	EncodeData(&encoderSend,listSocketId);
	EncodeData(&encoderSend,encoder);
	SendPacket(nSocketId,encoderSend);
}

void ServerCallbackGame::SendPacket(UInt32 nSocketId,UInt32 nSocketIdOnGate,PacketEncoder& encoder)
{
	PacketEncoder encoderSend;
	encoderSend.AddItem(Int32(1));
	encoderSend.AddItem(Int32(nSocketIdOnGate));
	EncodeData(&encoderSend,encoder);
	SendPacket(nSocketId,encoderSend);
}

void ServerCallbackGame::SendPacketToAll(UInt32 nSocketId,PacketEncoder& encoder)
{
	PacketEncoder encoderSend;
	encoderSend.AddItem(Int32(0));
	EncodeData(&encoderSend,encoder);
	SendPacket(nSocketId,encoderSend);
}
void ServerCallbackGame::Ping(UInt32 nSocketId)
{
	static PacketEncoder encoder;
	encoder.Clear();
	encoder.AddItem(Int32(1));
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(0));
	SendPacket(nSocketId, encoder);
}

void SendPacket(UInt32 nSocketId,std::list<Int32>& listSocketId,PacketEncoder& encoder)
{
	singleton<ServerCallbackGame>::instance().SendPacket(nSocketId,listSocketId,encoder);
}
void SendPacket(UInt32 nSocketId,UInt32 nSocketIdOnGate,PacketEncoder& encoder)
{
	singleton<ServerCallbackGame>::instance().SendPacket(nSocketId,nSocketIdOnGate,encoder);
}
void SendPacketToAll(UInt32 nSocketId,PacketEncoder& encoder)
{
	singleton<ServerCallbackGame>::instance().SendPacketToAll(nSocketId,encoder);
}
